@JsonIdentityInfo এর মাধ্যমে object references ব্যবহার

@JsonIdentityInfo এবং Object Identity - জ্যাকসন অ্যানোটেশন (Jackson Annotations) - Java Technologies

293

Jackson-এর @JsonIdentityInfo অ্যানোটেশনটি Circular References এবং Object References পরিচালনা করার জন্য ব্যবহৃত হয়। এটি বিশেষভাবে উপকারী যখন আপনার অবজেক্টের মধ্যে bi-directional references (একটি অবজেক্ট অন্য অবজেক্টকে রেফার করে এবং পরবর্তীতে সেই অবজেক্ট আবার প্রথমটি রেফার করে) থাকে এবং আপনি ঐ অবজেক্টগুলোর রেফারেন্সগুলো flat structure-এ JSON-এ প্রদর্শন করতে চান।

@JsonIdentityInfo এর মূল বৈশিষ্ট্য:

  1. Circular Reference Management:
    • এটি circular references (যেমন: Parent -> Child -> Parent) সমাধান করতে সহায়তা করে।
    • আপনার অবজেক্টের রেফারেন্সকে ID দিয়ে চিহ্নিত করে যাতে সেগুলো JSON-এ সঠিকভাবে ম্যানেজ করা যায়।
  2. Object References:
    • একাধিক অবজেক্টের মধ্যে রেফারেন্স শেয়ার করার জন্য ID ব্যবহার করতে সহায়তা করে।
    • একটি object থেকে অন্য object-এ রেফারেন্স করতে, JSON-এ একটি ইউনিক ID ব্যবহার করা হয়।
  3. Serialization:
    • Object-এর প্রতিটি রেফারেন্স ID হিসেবে JSON-এ সিরিয়ালাইজ করা হয়, যাতে circular reference এড়ানো যায় এবং ডুপ্লিকেট ডেটা তৈরি না হয়।

@JsonIdentityInfo এর বৈশিষ্ট্য:

  • generator: Object ID তৈরি করার জন্য কোন ObjectIdGenerator ব্যবহার করা হবে তা নির্ধারণ করে। এটি সাধারণত PropertyGenerator বা ObjectIdGenerators.UUIDGenerator হতে পারে।
  • property: ID ফিল্ডের নাম নির্ধারণ করে।

@JsonIdentityInfo এর উদাহরণ:

Scenario: Circular Reference এর সমস্যা সমাধান

ধরা যাক, আমাদের একটি Person এবং Address ক্লাস রয়েছে, যেখানে Person ক্লাসের একটি Address ফিল্ড এবং Address ক্লাসের একটি Person ফিল্ড রয়েছে। যদি আমরা এগুলোর মধ্যে bi-directional reference তৈরি করি, তাহলে circular reference সমস্যার সম্মুখীন হব।

Person ক্লাস (Parent):

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

public class Person {
    private int id;
    private String name;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    private Address address;

    public Person(int id, String name, Address address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

Address ক্লাস (Child):

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

public class Address {
    private int id;
    private String street;
    private Person person;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    private String city;

    public Address(int id, String street, String city, Person person) {
        this.id = id;
        this.street = street;
        this.city = city;
        this.person = person;
    }

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

Serialization Example:

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonIdentityInfoExample {
    public static void main(String[] args) throws Exception {
        // Create objects
        Person person = new Person(1, "John Doe", null);
        Address address = new Address(1, "1234 Elm St", "Springfield", person);

        // Set the Address object into the Person object
        person.setAddress(address);

        // Create ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        // Convert object to JSON
        String json = mapper.writeValueAsString(person);
        System.out.println("Serialized JSON: " + json);
    }
}

JSON Output:

{
  "id": 1,
  "name": "John Doe",
  "address": {
    "id": 1,
    "street": "1234 Elm St",
    "city": "Springfield",
    "person": 1
  }
}

ব্যাখ্যা:

  • @JsonIdentityInfo ব্যবহার করা হয়েছে Person এবং Address ক্লাসে।
  • address ফিল্ডের Person অবজেক্টের মধ্যে address ক্লাসের person রেফারেন্স আছে, কিন্তু JSON আউটপুটে circular reference এড়ানো হয়েছে।
  • @JsonIdentityInfo Object ID ব্যবহার করে circular references ম্যানেজ করেছে।
  • person এর ID হিসেবে 1 ব্যবহার করা হয়েছে, এবং address ফিল্ডের মধ্যে person এর রেফারেন্সকে 1 হিসেবে দেখানো হয়েছে।

@JsonIdentityInfo এর সুবিধা

  1. Circular Reference Handling:
    • Circular references (যেমন: parent -> child -> parent) সহজেই ID ব্যবহার করে পরিচালনা করা যায়।
  2. Object References Management:
    • Object references JSON-এ ID হিসেবে উল্লেখ করা হয়, যাতে ডুপ্লিকেট ডেটা এড়ানো যায়।
  3. Efficient Serialization:
    • একাধিক nested অবজেক্ট বা complex data structures প্রসেস করার সময় JSON আউটপুট ছোট এবং পরিষ্কার থাকে।
  4. Avoids Redundant Data:
    • JSON-এ একাধিক বার একই অবজেক্ট সিরিয়ালাইজ না হয়ে, শুধু ID দিয়ে সেই অবজেক্টের রেফারেন্স করা হয়।

  • @JsonIdentityInfo একটি গুরুত্বপূর্ণ অ্যানোটেশন যা Circular Reference এবং Object References সমাধানে ব্যবহৃত হয়।
  • এটি ID ব্যবহার করে nested বা bi-directional references সঠিকভাবে serializedeserialize করতে সহায়তা করে।
  • এটি JSON ডেটার কার্যকারিতা এবং পরিষ্কারতা বজায় রাখে, বিশেষত complex data structures-এ।
Content added By
Promotion

Are you sure to start over?

Loading...